package com.loubobooo.personal.blog.service.impl;

import com.loubobooo.personal.blog.entity.vo.CategoryVO;
import com.loubobooo.personal.blog.mapper.CategoryMapper;
import com.loubobooo.personal.blog.mapper.custom.ArticleMapperCustom;
import com.loubobooo.personal.blog.mapper.custom.CategoryMapperCustom;
import com.loubobooo.personal.blog.entity.Category;
import com.loubobooo.personal.blog.entity.vo.ArticleVO;
import com.loubobooo.personal.blog.entity.vo.ArticleListVo;
import com.loubobooo.personal.blog.page.PageResult;
import com.loubobooo.personal.blog.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * @Description: 用户管理service
 * @author: loubobooo
 * @date: 2018/3/23 15:43
 */
@Service
public class CategoryServiceImpl implements CategoryService {
	@Autowired
	private CategoryMapperCustom categoryMapperCustom;
	
	@Autowired
	private CategoryMapper categoryMapper;

	@Autowired
	private ArticleMapperCustom articleMapperCustom;

	@Override
	public Integer countCategory(Integer status)  {
		Integer categoryCount = categoryMapperCustom.countCategory(status);
		return categoryCount;
	}
	
	@Override
	public List<CategoryVO> listCategory(Integer status)  {
		List<CategoryVO> categoryVOList = categoryMapperCustom.listCategory(status);
		for(int i = 0; i< categoryVOList.size(); i++) {
			Integer cateId = categoryVOList.get(i).getCategoryId();
			Integer count = articleMapperCustom.countArticleByCategory(status,cateId);
			categoryVOList.get(i).setArticleCount(count);
		}

		return categoryVOList;
	}
	
	/**
	  * @Description: TODO:等待修改的内容
	  * @return     返回类型
	  * @date 2018/3/23 15:42
	  */
	@Override
	public List<ArticleListVo> listArticleWithCategoryByPage(Integer status,Integer pageNo, Integer pageSize,Integer cateId)  {
		List<ArticleListVo> articleListVoList = new ArrayList<ArticleListVo>();

		//获得分类的信息
		Category category = categoryMapper.selectByPrimaryKey(cateId);
		//如果没有这个分类，返回null
		if(category == null) {
			return null;
		}

		//分页显示
		PageResult pageResult = null;
		int totalCount = articleMapperCustom.countArticleByCategory(status,cateId);

		if (pageNo != null) {
			pageResult = new PageResult(totalCount, pageNo,pageSize);
		} else {
			pageResult = new PageResult(totalCount, 1,pageSize);
		}
		List<ArticleVO> articleVOList = categoryMapperCustom.listArticleWithCategoryByPage(status,cateId,pageResult.getStartPos(), pageResult.getPageSize());
		for(int i = 0; i< articleVOList.size(); i++) {
			ArticleListVo articleListVo = new ArticleListVo();
			
			//1、将文章信息装入 articleListVo
			ArticleVO articleVO = articleVOList.get(i);
			articleListVo.setArticleVO(articleVO);

			//2、将分类信息装到 articleListVoList 中
			List<CategoryVO> categoryVOList = new ArrayList<CategoryVO>();
			Integer parentCategoryId = articleVOList.get(i).getArticleParentCategoryId();
			Integer childCategoryId = articleVOList.get(i).getArticleChildCategoryId();
			CategoryVO categoryVO = categoryMapperCustom.getCategoryById(status, parentCategoryId);
			CategoryVO categoryVO2 = categoryMapperCustom.getCategoryById(status,childCategoryId);
			if(categoryVO !=null) {
				categoryVOList.add(categoryVO);
			}
			if(categoryVO2 !=null) {
				categoryVOList.add(categoryVO2);
			}
			articleListVo.setCategoryVOList(categoryVOList);

			articleListVoList.add(articleListVo);
		}
        //如果该分类还没有文章
        if(totalCount != 0) {
            //2、将Page信息存储在第一个元素中
            articleListVoList.get(0).setPageResult(pageResult);
        }
		return articleListVoList;
	}

	@Override
	public CategoryVO getCategory(Integer status,Integer id)  {
		CategoryVO categoryVO = categoryMapperCustom.getCategoryById(status,id);
		return categoryVO;
	}

    @Override
    public void deleteCategory(Integer id)  {
        categoryMapperCustom.deleteCategory(id);
    }

    @Override
    public CategoryVO getCategoryById(Integer status,Integer id)  {
		CategoryVO categoryVO = categoryMapperCustom.getCategoryById(status,id);
        return categoryVO;
    }

	@Override
	public void insertCategory(Category category)  {
		categoryMapper.insertSelective(category);
	}

	@Override
	public void updateCategory(Category category)  {
		categoryMapper.updateByPrimaryKeySelective(category);
	}

	@Override
	public Category getCategoryByName(String name)  {
		Category category = categoryMapperCustom.getCategoryByName(name);
		return category;
	}

}
